package erwiki.ui.app.admin.dialogs.hierachychange;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.elwiki_data.WikiPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;

public class PagesSelectDialog extends Dialog {

	private static final long serialVersionUID = -5240289352666121675L;

	private Boolean isMultiple;

	private List<WikiPage> inputData;

	private List<?> selected;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param isMultiple  Определяет результат выбора: одна/множество страниц.
	 * @param inputData   TODO
	 */
	public PagesSelectDialog(Shell parentShell, Boolean isMultiple, List<WikiPage> inputData) {
		super(parentShell);
		this.isMultiple = isMultiple;
		this.inputData = inputData;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText((this.isMultiple.booleanValue()) ? "Выбор страниц Wiki" : "Выбор страницы Wiki");
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));

		TreeViewer treeViewer = new TreeViewer(container, SWT.BORDER | //
				((this.isMultiple.booleanValue()) ? SWT.MULTI : SWT.SINGLE));
		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				treeSelectionChanged(event);
			}
		});
		Tree tree = treeViewer.getTree();
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		treeViewer.setContentProvider(new ViewContentProvider());
		treeViewer.setLabelProvider(new ViewLabelProvider());
		treeViewer.setSorter(new ViewerSorter() {
			private static final long serialVersionUID = 8907584367998250840L;

			@Override
			public int compare(Viewer viewer1, Object e1, Object e2) {
				if (e1 instanceof WikiPage && e2 instanceof WikiPage) {
					String s1 = ((WikiPage) e1).getName();
					String s2 = ((WikiPage) e2).getName();
					return s1.compareTo(s2);
				}
				return super.compare(viewer1, e1, e2);
			}

		});
		treeViewer.setInput(this.inputData);
		treeViewer.expandToLevel(2);

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Вввод", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Отмена", false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(460, 900);
	}

	protected void treeSelectionChanged(SelectionChangedEvent event) {
		ISelection selection = event.getSelection();
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection ss = (IStructuredSelection) selection;
			this.selected = ss.toList();
		}
	}

	public List<?> getSelectedPages() {
		return this.selected;
	}

}
